package com.example.jsonarry.nanningtravel.adapter;

import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.jsonarry.nanningtravel.R;

public class NavigationRecyclerAdapter extends RecyclerView.Adapter<NavigationRecyclerAdapter.MyViewHolder>
{

    private final Context context;
    private final List<String> list;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public NavigationRecyclerAdapter(Context context, List<String> list)
    {
        this.context = context;
        this.list = list;
    }

    class MyViewHolder extends ViewHolder
    {
        TextView tv;

        public MyViewHolder(View view)
        {
            super(view);
            tv = (TextView) view.findViewById(R.id.num);
        }

    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position)
    {
        // 绑定数据给view
        holder.tv.setText(list.get(position));
        //绑定点击事件
        if (mOnItemClickListener != null)
        {
            holder.itemView.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    mOnItemClickListener.onItemClick(v, position);
                }
            });
        }
        if (mOnItemLongClickListener != null)
        {
            holder.itemView.setOnLongClickListener(new OnLongClickListener()
            {

                @Override
                public boolean onLongClick(View v)
                {
                    mOnItemLongClickListener.onItemLongClick(v, position);
                    return true;
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position)
    {
        // 创建视图
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, true);
        //		MyViewHolder holder = new MyViewHolder(View.inflate(context, R.layout.item, null));
        return new MyViewHolder(view);
    }

    public interface OnItemClickListener
    {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener
    {
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener)
    {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemClickLongListener(OnItemLongClickListener listener)
    {
        this.mOnItemLongClickListener = listener;
    }

}
